/*
 * Copyright (c) 2017. Rushang Co.Ltd All Rights Reserved.
 * 项目名称:rsdp-rsdl
 * 文件名称:index.js
 * 主页js
 *
 * @create 2017-09-06
 * @author zhangjun
 */

$(document).keypress(function (event) {
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if (keycode == '13') {
        submitForm();
    }

});
$(function () {

    /*布局部分*/
    $('#theme-login-layout').layout({
        fit: true
        /*布局框架全屏*/
    });
    $('#username').textbox({
        required: true,
        missingMessage: "请输入用户名"
    });
    $('#password').textbox({
        type: "password",
        //prompt : '请输入密码',
        required: true,
        missingMessage: "请输入密码"
    });
    $('#vcode').textbox({
        //prompt : '请输入用户名',
        required: true,
        missingMessage: "请输入验证码",
        tipPosition: "left"
    });

    $('.submit').click(function () {//表单提交
        submitForm();
    });

    $('.reset').click(function () {//表单重置
        $("#form").form('clear');
    });


    $('#kaptchaImage').click(function () {//生成验证码
        $(this).hide().attr('src', ctxPath + '/kaptcha?p=' + Math.floor(Math.random() * 100)).fadeIn();
        event.cancelBubble = true;
    });

});

function submitForm() {
    $('#form').form('submit', {
        url: ctxPath + "/login",
        onSubmit: function () {
            var res = $(this).form('validate');
            res ? $.messager.progress() : "";
            return res;
        },
        success: function (data) {
            $.messager.progress('close');
            try {
                var data = eval('(' + data + ')');
                if (data.code == 200) {
                    window.location = ctxPath + data.url;
                } else {
                    $("#kaptchaImage").hide().attr('src', ctxPath +
                        '/kaptcha?p=' + Math.floor(Math.random() * 100)
                    ).
                    fadeIn();
                    layer.msg(data.msg, {time: 2000});
                }
            } catch (e) {
                $.insdep.error(data);
            }

        }
    });
}